#include <iostream>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <cstring>

int main() {
    const char* fifo = "mypipe";
    
    // 创建命名管道，如果已存在则忽略错误
    if (mkfifo(fifo, 0666) == -1) {
        perror("mkfifo");
    }

    int fd = open(fifo, O_WRONLY);
    if (fd < 0) {
        perror("open");
        return 1;
    }
    const char* message = "I am process A";
    ssize_t written = write(fd, message, strlen(message) + 1);  // 加上 \0 是为了 null-terminate the string
    if (written < 0) {
        perror("write");
        close(fd);
        return 1;
    } else {
        std::cout << "Sent: " << message << std::endl;
    }

    close(fd);
    return 0;
}
